﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class outbox : System.Web.UI.Page
{
    static int userid;
    static int groupid;
    static int FlagDel = 0;
    static string sentto = "";
    static DataTable dtreplies;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindGrid();
        }
    }
    protected void ImgBtnDelete_Click(object sender, ImageClickEventArgs e)
    {
        GridViewRow row = (GridViewRow)(sender as Control).Parent.Parent;
        int index = row.RowIndex;
        Label lblmsgid = GridViewSentMessages.Rows[index].FindControl("LblMsgID") as Label;
        SqlHelper.ExecuteNonQuery(GlobalVariable.con, CommandType.Text, "delete from tbl_SentMessages where msgid='" + Convert.ToInt32(lblmsgid.Text) + "'");
        DataTable dt = SqlHelper.ExecuteDatatable(GlobalVariable.con, CommandType.Text, "select MsgId,MsgFrom,MsgTo,MsgText,MsgStatus,SentDateTime,ScheduleDateTime,smsDeliveryTime from tbl_SentMessages");
        GridViewSentMessages.DataSource = dt;
        GridViewSentMessages.DataBind();
        if (GridViewSentMessages.Rows.Count > 0)
        {
            BtnDeleteAll.Visible = true;
        }
        else
        {
            BtnDeleteAll.Visible = false;
        }
    }
    protected void BtnDeleteAll_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridViewSentMessages.Rows.Count - 1; i++)
        {
            CheckBox chkboxselect = GridViewSentMessages.Rows[i].FindControl("ChkBoxSelect") as CheckBox;
            Label lblmsgid = GridViewSentMessages.Rows[i].FindControl("LblMsgID") as Label;
            if (chkboxselect.Checked)
            {
                SqlHelper.ExecuteNonQuery(GlobalVariable.con, CommandType.Text, "delete from tbl_SentMessages where msgid='" + Convert.ToInt32(lblmsgid.Text) + "'");
            }
        }
        bindGrid();
        //DataTable dt = SqlHelper.ExecuteDatatable(GlobalVariable.con, CommandType.Text, "select MsgId,MsgFrom,MsgTo,MsgText,MsgStatus,SentDateTime,ScheduleDateTime,smsDeliveryTime from tbl_SentMessages");
        //GridViewSentMessages.DataSource = dt;
        //GridViewSentMessages.DataBind();
        //if (GridViewSentMessages.Rows.Count > 0)
        //{ BtnDeleteAll.Visible = true; }
        //else
        //{
        //    BtnDeleteAll.Visible = false;
        //}
    }
    protected void LnkBtnViewSentMessages_Click(object sender, EventArgs e)
    {
        Panelsentmessages.Visible = true;
        bindGrid();
    }
    protected void GridViewSentMessages_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewSentMessages.PageIndex = e.NewPageIndex;
        bindGrid();
    }
    protected void HeaderLevelCheckBox_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void GridViewSentMessages_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression == ViewState["SentDateTime"].ToString())
        {
            if (ViewState["Sort_Order"].ToString() == "ASC")
                RebindData(e.SortExpression, "DESC");
            else
                RebindData(e.SortExpression, "ASC");
        }
        else
            RebindData(e.SortExpression, "ASC");
    }
    private void RebindData(string sColimnName, string sSortOrder)
    {
        DataTable dt = (DataTable)Cache["dt"];
        dt.DefaultView.Sort = sColimnName + " " + sSortOrder;
        GridViewSentMessages.DataSource = dt;
        GridViewSentMessages.DataBind();
        ViewState["SentDateTime"] = sColimnName;
        ViewState["Sort_Order"] = sSortOrder;
    }
    //protected void LnkBtnSndAgn_Click(object sender, EventArgs e)
    //{
    //    Session[GlobalVariable.Session.MsgText.ToString()] = LblMsgText.Text;
    //    Session[GlobalVariable.Session.MsgTo.ToString()] = sentto;
    //    Response.Redirect("sendsms.aspx");
    //}

    public void bindGrid()
    {
        DataTable dt = SqlHelper.ExecuteDatatable(GlobalVariable.con, CommandType.Text, "select MsgId,MsgFrom,MsgTo,MsgText,MsgStatus,SentDateTime,ScheduleDateTime,smsDeliveryTime from tbl_SentMessages where MsgStatus<>'scheduled'");
        GridViewSentMessages.DataSource = dt;
        GridViewSentMessages.DataBind();
        Cache["dt"] = dt;
        ViewState["SentDateTime"] = "SentDateTime";
        ViewState["Sort_Order"] = "ASC";
        GridViewSentMessages.Sort("SentDateTime", SortDirection.Descending);
        if (GridViewSentMessages.Rows.Count > 0)
        { BtnDeleteAll.Visible = true; }
        else
        {
            BtnDeleteAll.Visible = false;
        }

        DataTable dtsch = SqlHelper.ExecuteDatatable(GlobalVariable.con, CommandType.Text, "select MsgId,MsgFrom,MsgTo,MsgText,MsgStatus,SentDateTime,ScheduleDateTime,smsDeliveryTime from tbl_SentMessages where MsgStatus='scheduled'");
        GridView1.DataSource = dtsch;
        GridView1.DataBind();
        Cache["dtsch"] = dtsch;
        ViewState["SentDateTime1"] = "SentDateTime";
        ViewState["Sort_Order1"] = "ASC";
        GridView1.Sort("SentDateTime", SortDirection.Descending);
        //if (GridView1.Rows.Count > 0)
        //{
        //    btnDeleteScheduled.Visible = true;
        //}
        //else
        //{
        //    btnDeleteScheduled.Visible = false;
        //}

    }
    protected void btnDeleteScheduled_Click(object sender, EventArgs e)
    {
        int deletednumbers = 0;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox chkboxselect = GridView1.Rows[i].FindControl("ChkBoxSelectSch") as CheckBox;
            Label lblmsgid = GridView1.Rows[i].FindControl("LblMsgIDSch") as Label;
            if (chkboxselect.Checked)
            {
                SqlHelper.ExecuteNonQuery(GlobalVariable.con, CommandType.Text, "delete from tbl_SentMessages where msgid='" + Convert.ToInt32(lblmsgid.Text) + "'");
                deletednumbers++;
            }
        }
        bindGrid();
        string SelectGroup = "" + deletednumbers + " messages deleted from list.";
        Page.Controls.Add(new LiteralControl("<script language='javascript'> window.alert('" + SelectGroup + "')</script>"));
    }
    protected void btnOptOutSelected_Click(object sender, EventArgs e)
    {
        int countopt = 0;
        int noop = 0;
        int totaln = 0;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox chkboxselect = GridView1.Rows[i].FindControl("ChkBoxSelectSch") as CheckBox;
            Label lblmsgid = GridView1.Rows[i].FindControl("LblMsgIDSch") as Label;
            Label lblMsgTo = GridView1.Rows[i].FindControl("lblMsgTo") as Label;
            if (chkboxselect.Checked)
            {
                totaln++;
                object nmbr = SqlHelper.ExecuteScalar(GlobalVariable.con, CommandType.Text, "Select ContactId from tbl_groupcontacts where MobileNumber='" + lblMsgTo.Text + "'");
                if (nmbr != null)
                {
                    SqlHelper.ExecuteNonQuery(GlobalVariable.con, CommandType.Text, "update tbl_groupcontacts set Unsubscribed='true' where MobileNumber = '" + lblMsgTo.Text + "'");
                    countopt++;
                }
                else
                {
                    noop++;
                }
            }
        }
        bindGrid();
        string SelectGroup = "";
        if (noop == 0)
        {
            SelectGroup = "Total " + totaln + " contacts selected. " + countopt + " numbers opted out from database.";
        }
        else
        {
            SelectGroup = "Total " + totaln + " contacts selected. " + countopt + " numbers opted out from database.. " + noop + " contacts does not exist in database.";
        }
        Page.Controls.Add(new LiteralControl("<script language='javascript'> window.alert('" + SelectGroup + "')</script>"));
    }
    protected void ddlShowRecords_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlShowRecords.SelectedIndex > 0)
        {
            GridView1.PageSize = Convert.ToInt32(ddlShowRecords.SelectedValue);
            bindGrid();
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression == ViewState["SentDateTime1"].ToString())
        {
            if (ViewState["Sort_Order1"].ToString() == "ASC")
                RebindData(e.SortExpression, "DESC");
            else
                RebindData(e.SortExpression, "ASC");
        }
        else
            RebindData(e.SortExpression, "ASC");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bindGrid();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        int CanceledMessages = 0;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox chkboxselect = GridView1.Rows[i].FindControl("ChkBoxSelectSch") as CheckBox;
            Label lblmsgid = GridView1.Rows[i].FindControl("LblMsgIDSch") as Label;
            if (chkboxselect.Checked)
            {
                SqlHelper.ExecuteNonQuery(GlobalVariable.con, CommandType.Text, "update tbl_SentMessages set MsgStatus='" + GlobalVariable.MsgStatus.Canceled.ToString() + "' where msgid='" + Convert.ToInt32(lblmsgid.Text) + "'");
                CanceledMessages++;
            }
        }
        bindGrid();
        string SelectGroup = "" + CanceledMessages + " messages are canceled.";
        Page.Controls.Add(new LiteralControl("<script language='javascript'> window.alert('" + SelectGroup + "')</script>"));
    }
    protected void ddlRecords_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlRecords.SelectedIndex > 0)
        {
            GridViewSentMessages.PageSize = Convert.ToInt32(ddlRecords.SelectedValue);
            bindGrid();
        }
    }
}
